﻿namespace FFF.Test.YCmd.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Threading.Tasks;
    using Abp;
    using Abp.Application.Services.Dto;
    using Abp.Castle.Logging.Log4Net;
    using Abp.Dependency;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore;
    using Abp.EntityFrameworkCore.Extensions;
    using Abp.Json;
    using Abp.Linq.Dynamic.Extensions;
    using AutoMapper.Configuration;
    using Castle.Facilities.Logging;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    using FFF.Test.Application.AppServices.Demo;
    using FFF.Test.Application.DomainServices.Demo;
    using FFF.Test.Application.Repositories.Demo;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Entity.Demo;
    using FFF.Test.Entity.Demo.Dto;
    using FFF.Test.Entity.Demo.Function;
    using FFF.Test.Entity.Demo.Proc;
    using FFF.Test.Entity.Demo.View;
    using FFF.Test.Util;
    using FFF.Test.Util.Elastic;
    using FFF.Test.Util.Elastic.Dto;
    using Z.Expressions;
    using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;

    internal class Program
    {

        private static void Main(string[] args)
        {

            using (AbpBootstrapper _bootstrapper = AbpBootstrapper.Create<AdbTestModule>())
            {

                _bootstrapper.Initialize();
                _bootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config"));
                IUnitOfWorkManager unitOfWorkManager = _bootstrapper.IocManager.Resolve<IUnitOfWorkManager>();

                DemoStudentEntity sskk = new DemoStudentEntity() { StudentName = "55" };

                object result22 = "X.StudentName".Execute(new { X = sskk });


                UnitOfWorkOptions options = new UnitOfWorkOptions()
                {
                    IsTransactional = false
                };
                using (IUnitOfWorkCompleteHandle contactUnitOfWork = unitOfWorkManager.Begin())
                {
                    IDbContextProvider<MyDbContext> dbContextProvider = _bootstrapper.IocManager.Resolve<IDbContextProvider<MyDbContext>>();
                    var db = dbContextProvider.GetDbContext();
                    db.DemoStudentTe.Where(x => x.Id == new Guid("42A3D392-4423-4F78-0753-08D6DEA42072")).DeleteFromQuery();
                    contactUnitOfWork.Complete();
                }

                // BulkOptionHelper.BullkInsertTable();




                //string sql = "select top 1 StuName from Demo_Student";

                //DemoStudentEntity sskk = new DemoStudentEntity() { StudentName = "55" };

                // object result22 = "X.StudentName".Execute(new { X = sskk });

                //UnitOfWorkOptions options = new UnitOfWorkOptions()
                //{
                //    IsTransactional = false
                //};

                //using (IUnitOfWorkCompleteHandle contactUnitOfWork = unitOfWorkManager.Begin())
                //{
                //MyDbContext db = _bootstrapper.IocManager.Resolve<IDbContextProvider<MyDbContext>>().GetDbContext();
                //string ss = db.GetFlowNumber("SH_Device");
                //var kk123 = db.ViewDemoStudent.Where(x => x.CreateTime != null)
                //    .GroupBy(x => new { x.CreateTime.Value.Year, x.CreateTime.Value.Month }, (key, lst) => new
                //    {
                //        Year = key.Year,
                //        Month = key.Month,
                //        Count = lst.Count()
                //    }).ToList();
                //string s = db.ExecuteScalarAsync<string>(sql).Result;
                //DataTable dt = db.GetDataTableAsync(sql).Result;
                //List<DemoStudentEntity> student = db.QueryListAsync<DemoStudentEntity>(sql).Result;
                //PagedInputDto inputDto2 = new PagedInputDto()
                //{
                //    Order = "StuName",
                //    PageIndex = 1,
                //    PageSize = 10
                //};
                //var mmm123 = db.GetDbContext().ViewDemoStudent.GetPageAsync<ViewDemoStudent, ViewDemoStudent>(inputDto2).Result.DataList;

                //  var m123 = db.GetDbContext().ViewDemoStudent.ToList();

                // List<object> lst = db.GetDbContext().ViewDemoStudent.Select("new(StuName as StuName2)").Cast<object>().ToList();

                // string s123 = JsonConvert.SerializeObject(lst);
                //  }



                //EsDocument document = new EsDocument()
                //{
                //    Id = 10,
                //    Author = "yudm",
                //    Content = "kkkkk12321132",
                //    CreateTime = DateTime.Now,
                //    Title = "test",
                //    Type = "core;js"
                //};
                //string jsonStr = JsonSerializationHelper.SerializeWithType(document); // 转str
                //// var jsonStr = document.toJsonStr(); // 转str 不会携带类的名称
                //EsDocument viewUser = JsonSerializationHelper.DeserializeWithType<EsDocument>(jsonStr); // 转对象

                //Nest.IBulkResponse res = ElasticHelper.InsertAsync(document).Result;
                //document.Author = "yudm22";
                //Nest.IBulkResponse res2 = ElasticHelper.UpdateAsync(document).Result;

                //IReadOnlyCollection<EsDocument> result = ElasticHelper.EsClient.Search<EsDocument>(ss => ss
                //.From(0)
                //.Size(10)
                //.Query(q => q.Match(mm => mm.Field(f => f.Title).Query("test")))).Documents;

                //Nest.IBulkResponse resDelete = ElasticHelper.DeleteAsync<EsDocument>(document.Id.ToString()).Result;


                //using (IUnitOfWorkCompleteHandle contactUnitOfWork = unitOfWorkManager.Begin())
                //{

                //    PagedInputDto inputDto2 = new PagedInputDto()
                //    {
                //        Order = "StuName",
                //        PageIndex = 1,
                //        PageSize = 10
                //    };
                //    var mmm123 = db.GetDbContext().ViewDemoStudent.GetPageAsync<ViewDemoStudent, ViewDemoStudent>(inputDto2).Result.DataList;

                //    var m123 = db.GetDbContext().ViewDemoStudent.ToList();

                //    List<object> lst = db.GetDbContext().ViewDemoStudent.Select("new(StuName as StuName2)").Cast<object>().ToList();

                //    string s123 = JsonConvert.SerializeObject(lst);
                //}

                // IDemoStudentAppService service = _bootstrapper.IocManager.Resolve<IDemoStudentAppService>();
                // var oo = service.GetAllListDto().Result;

                //FnDemoSplitarr fnDemoSplitarr2 = new FnDemoSplitarr()
                //{
                //    Arr = "1,2,3,4",
                //    Split = ","
                //};
                //List<FnDemoSplitarrTableInfo> m2 = fnDemoSplitarr2.QueryList();

                //FnDemoGetHello fnDemoGetHello = new FnDemoGetHello()
                //{
                //    Str = "123"
                //};
                //string kk = fnDemoGetHello.ExecuteScalar<string>();

                //UspDemoGetDemoStudent proc = new UspDemoGetDemoStudent()
                //{
                //    StuName = "bbb"

                //};
                //List<DemoStudentEntity> m = proc.QueryList<DemoStudentEntity>();


                //Stopwatch watch = new Stopwatch();
                //watch.Start();

                //// service.GetAllListDto().Wait();
                //watch.Stop();
                //Console.WriteLine("用时{0}毫秒", watch.ElapsedMilliseconds);

                // var service = _bootstrapper.IocManager.Resolve<IDemoClassAppService>();
                // service.Delete(new System.Guid("6E45C2B7-EB7E-4978-9795-05CCADCA47E3")).Wait();
                // service.Delete(new System.Guid("258F1719-D8C7-402C-85B4-420AA6907949")).Wait();
                // service.GetAllListDto().Wait();

                //DMStudentEntity dMStudentEntity = new DMStudentEntity()
                //{
                //    StuName = "!23"
                //};
            }
        }

    }

    public class TestA<T2>
    {
        public static T Get<T>(object obj)
        {
            return (T)obj;
        }

        public T2 GetT2(object obj)
        {
            return (T2)obj;
        }
    }
}
